﻿#include "snake.h"
//游戏的初始化
// 1.设置窗口大小
    //2.光标隐藏
    //3.打印欢迎界面
	//4.功能介绍
	//5.绘制地图
	//6.创建蛇
	//7.创建食物
	//8.设置游戏的相关信息
void SetPos(short x, short y)
{
	//获取标准输出设备的句柄
	HANDLE houtput = NULL;
	houtput = GetStdHandle(STD_OUTPUT_HANDLE);
	//定位光标位置
	COORD pos = { x,y };
	SetConsoleCursorPosition(houtput, pos);
}
void WelcomeToGame()
{
	SetPos(40, 14);
	wprintf(L"欢迎来到贪吃蛇小游戏\n");
	SetPos(42, 20);
	system("pause");
	system("cls");
	SetPos(25, 14);
	wprintf(L"用↑.↓.←.→来控制蛇的移动,按F3加速,按F4减速\n");
	SetPos(25, 15);
	wprintf(L"加速能够得到更高的分数\n");
	SetPos(42, 20);
	system("pause");
	system("cls");
}
void CreatMap()
{
	//上
	int i = 0;
	for (i = 0; i < 29; i++)
	{
		wprintf(L"%lc", WALL);
	}
	//下
	SetPos(0, 26);
	for (i = 0; i < 29; i++)
	{
		wprintf(L"%lc", WALL);
	}
	//左
	for (i = 1; i <= 25; i++)
	{
		SetPos(0, i);
		wprintf(L"%lc", WALL);
	}
	//右
	for (i = 1; i <= 25; i++)
	{
		SetPos(56, i);
		wprintf(L"%lc", WALL);
	}
	
}
//初始化蛇身
void InitSnake(pSnake ps)
{
	pSnakeNode cur = NULL;
	int i = 0;
	for (i = 0; i < 5; i++)
	{
		cur = (pSnakeNode)malloc(sizeof(SnakeNode));
		if (cur == NULL)
		{
			perror("InitSnake malloc");
			return;
		}
		cur->x = POS_X + 2 * i;
		cur->y = POS_Y;
		cur->next = NULL;
		//头插法插入链表
		if (ps->_pSnake == NULL)//空链表
		{
			ps->_pSnake = cur;
		}
		else {//非空链表
			cur->next = ps->_pSnake;
			ps->_pSnake = cur;
		}
	}
	cur = ps->_pSnake;
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}
	//设置贪吃蛇属性
	ps->_dir = RIGHT;//方向默认向右
	ps->_score = 0;//总成绩
	ps->_food_weight = 10;//一个食物的分数
	ps->_sleep_time = 200;//单位毫秒
	ps->_status = OK;//蛇的状态
	
}
void CreatFood(pSnake ps)
{
	
	//生成x是2的倍数
	//x:2-54
	//y:1-25
	int x = 0;
	int y = 0;
again:
	do
	{
		x = rand() % 53 + 2;
		y = rand() % 25 + 1;
	} while (x%2!=0); 
	//x和y的坐标不能和蛇的身体坐标冲突
	pSnakeNode cur = ps->_pSnake;
	while (cur)
	{
		if (cur->x == x && cur->y == y)
		{
			goto again;
		}
		cur = cur->next;
	}
	pSnakeNode pFood = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (pFood == NULL)
	{
		perror("CreatFood pFood malloc");
		return;
	}
	pFood->x = x;
	pFood->y = y;
	pFood->next = NULL;
	SetPos(x, y);
	wprintf(L"%lc", FOOD);
	ps->_pFood = pFood;
}
void GameStart(pSnake ps)
{
	//1.设置窗口大小
	system("mode con cols=100 lines=30"); 
	system("title 贪吃蛇");
	//2.光标隐藏
	HANDLE houtput=GetStdHandle(STD_OUTPUT_HANDLE);//获取句柄
	//隐藏光标操作
	CONSOLE_CURSOR_INFO CursorInfo;
	GetConsoleCursorInfo(houtput, &CursorInfo);//获取控制台光标信息
	CursorInfo.bVisible = false;//隐藏控制台光标
	SetConsoleCursorInfo(houtput, &CursorInfo);//设置控制台光标状态
	
   //3.打印欢迎界面和功能介绍
	WelcomeToGame();
   //4.绘制地图
	CreatMap();
   //5.创建蛇
    //初始化蛇身
  InitSnake(ps);
   //6.创建食物
  CreatFood(ps);
  
}




//游戏的运行逻辑
//打印帮助信息
void PrintfHelpInfo()
{
	SetPos(64,14);
	wprintf(L"%ls", L"不能穿墙，不能撞到自己");
	SetPos(64, 15);
	wprintf(L"%ls", L"用↑.↓.←.→来控制蛇的移动");
	SetPos(64, 16);
	wprintf(L"%ls", L"按F3加速,按F4减速");
	SetPos(64, 17);
	wprintf(L"%ls",L"按ESC退出游戏，按空格暂停游戏");
	SetPos(64, 19);
	wprintf(L"%ls", L"版权归制作者所有，抄袭者必纠");
}

#define KEY_PRESS(vk) ((GetAsyncKeyState(vk)&1)?1:0)
void Pause()
{
	while(1)
	{
		Sleep(200);
		if (KEY_PRESS(VK_SPACE))
		{
			break;
		}
	} 
}
int NextisFood(pSnakeNode NextNode, pSnake ps)
{
	if (NextNode->x == ps->_pFood->x && NextNode->y == ps->_pFood->y)
	{
		return 1;
	 }
	else
	{
		return 0;
	}
}
void EatFood(pSnakeNode pn, pSnake ps)
{
	//头插法
	ps->_pFood->next = ps->_pSnake;
	ps->_pSnake = ps->_pFood;
	//释放下一个位置的节点
	free(pn);
	pn = NULL;
	//打印蛇
	pSnakeNode cur = ps->_pSnake;
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}
	ps->_score+=ps->_food_weight;//计算分数
	//重新创建食物
	CreatFood(ps);
}
void NoFood(pSnakeNode pn, pSnake ps)
{
	//头插法
	pn->next = ps->_pSnake;
	ps->_pSnake =pn;
	//释放尾结点
	pSnakeNode cur = ps->_pSnake;
	while (cur->next->next!=NULL)
	{
		cur = cur->next;
	}
	//把最后一个节点打印成空格
	SetPos(cur->next->x, cur->next->y);
	printf("  ");
	//释放最后一个节点
	free(cur->next);
	cur->next = NULL;
	//打印
	cur = ps->_pSnake;
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}
}
void killbywall(pSnake ps)
{
	if (ps->_pSnake->x == 0 || ps->_pSnake->x == 56 ||
		ps->_pSnake->y == 0 || ps->_pSnake->y == 26)
	{
		ps->_status = KILL_BY_WALL;
	}
}
void killbyself(pSnake ps)
{
	pSnakeNode cur = ps->_pSnake->next;
	while (cur)
	{
		if (ps->_pSnake->x == cur->x && ps->_pSnake->y == cur->y)
		{
			ps->_status = KILL_BY_SELF;
			break;
		}
		cur = cur->next;
	}
}
void SnakeMove(pSnake ps)
{
	//创建指针，指向下一个蛇即将到达的结点
	pSnakeNode NextNode = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (NextNode == NULL)
	{
		perror("SnakeMove:malloc()");
		return;
	}
	switch (ps->_dir)
	{
	case UP:
		NextNode->x = ps->_pSnake->x;
		NextNode->y = ps->_pSnake->y - 1;
		break;
	case DOWN:
		NextNode->x = ps->_pSnake->x;
		NextNode->y = ps->_pSnake->y +1;
		break;
	case LEFT:
		NextNode->x = ps->_pSnake->x-2;
		NextNode->y = ps->_pSnake->y;
		break;
	case RIGHT:
		NextNode->x = ps->_pSnake->x +2;
		NextNode->y = ps->_pSnake->y;
		break;
	}
	//判断下一个节点是否为食物
	if (NextisFood(NextNode, ps)==1)
	{
		EatFood(NextNode,ps);
		
	}
	else
	{
		NoFood(NextNode, ps);
	}
	
	//检测是否撞墙
	killbywall(ps);
	//检测是否撞到自己
	killbyself(ps);


	
}
void GameRun(pSnake ps,int* temp)
{
	//打印帮助信息
	PrintfHelpInfo(); 
	do
	{
		//打印最高纪录
		SetPos(64, 9);
		printf("最高纪录:%d\n", *temp);
		//打印总分数和食物的分值
		SetPos(64, 10);
		printf("总分数:%d\n", ps->_score);
		SetPos(64, 11);
		printf("当前食物的分值:%2d\n", ps->_food_weight);
		//检测按键情况，改变蛇的状态
		if (KEY_PRESS(VK_UP)&&ps->_dir!=DOWN)
		{
			ps->_dir = UP;
		}
		else if (KEY_PRESS(VK_DOWN)&&ps->_dir!=UP)
		{
			ps->_dir = DOWN;
		}
		else if (KEY_PRESS(VK_LEFT) && ps->_dir != RIGHT)
		{
			ps->_dir = LEFT;
		}

		else if (KEY_PRESS(VK_RIGHT) && ps->_dir != LEFT)
		{
			ps->_dir = RIGHT;
		}
		else if (KEY_PRESS(VK_SPACE))
		{
			//暂停游戏
			Pause(); 
		}
		else if (KEY_PRESS(VK_ESCAPE))
		{
			//正常退出游戏
			ps->_status = END_NORMAL;

		}
		else if (KEY_PRESS(VK_F3))
		{
			//加速
			if (ps->_sleep_time > 80)
			{
				ps->_sleep_time -= 30;
				ps->_food_weight += 3;
			}
		}
		else if (KEY_PRESS(VK_F4))
		{
			//减速
			if (ps->_food_weight > 1)
			{
				ps-> _sleep_time += 30;
				ps->_food_weight -= 3;
			}
		}
		//贪吃蛇走一步
		SnakeMove(ps);
		Sleep(ps->_sleep_time);
	} while (ps->_status==OK);
}



void GameEnd(pSnake ps)
{
	SetPos(24, 12);
	switch (ps->_status)
	{
	case END_NORMAL:
		printf("你已主动结束游戏");
		break;
	case KILL_BY_WALL:
		printf("你撞到墙上，游戏结束");
		break;
	case KILL_BY_SELF:
		printf("你撞到自己，游戏结束");
		break;
	}
	//释放每一个节点
	pSnakeNode cur = ps->_pSnake;
	while (cur)
	{
		pSnakeNode del = cur;
		cur = cur->next;
		free(del);
		del = NULL;
	}
}